﻿using System;
using System.Net.Mail;
using System.Text.RegularExpressions;


namespace Utility.Net
{
    public class Mail
    {
        /// <summary>
        /// sprawdza poprawnosc syntaktyczną adresu e-mail
        /// </summary>
        /// <param name="sEmail"></param>
        /// <returns></returns>
        public static bool IsValidEmailAddress(string sEmail)
        {
            if (sEmail == null)
            {
                return false;
            }

            int nFirstAT = sEmail.IndexOf('@');
            int nLastAT = sEmail.LastIndexOf('@');

            if ((nFirstAT > 0) && (nLastAT == nFirstAT) &&
            (nFirstAT < (sEmail.Length - 1)))
            {
                // address is ok regarding the single @ sign
                return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// wysyła wiadomość email pod podany adres
        /// </summary>
        /// <param name="pGmailEmail">dla faktur elektronicznych: e-faktura@podpisani.pl</param>
        /// <param name="pGmailPassword">dla e-faktura@podpisani.pl: okm123</param>
        /// <param name="pTo"></param>
        /// <param name="pSubject"></param>
        /// <param name="pBody"></param>
        /// <param name="pAttachmentPath"></param>
        /// <returns></returns>
        public static bool SendEmail(
            string pGmailEmail,
            string pGmailPassword,
            string pTo,
            string pSubject,
            string pBody,
            string pAttachmentPath,
            bool async,
            string bccMail)
        {
            return SendEmail(pGmailEmail, pGmailPassword, string.Empty, string.Empty, pTo, pSubject, pBody, pAttachmentPath, async, bccMail);
        }

        /// <summary>
        /// wysyła wiadomośc e-mail pod podany adres
        /// </summary>
        /// <param name="pGmailEmail">login do serwera</param>
        /// <param name="pGmailPassword">hasło do konta</param>
        /// <param name="pFromEmail">adres email od kogo ma przyjść wiadomość (w szczegolnosci pGmailEmail = pFromEmail)</param>
        /// <param name="pFromName">Nazwa od kogo pochodzi wiadomosc</param>
        /// <param name="pTo">do kogo kierowana</param>
        /// <param name="pSubject">temat wiadomości</param>
        /// <param name="pBody">treść wiadomości</param>
        /// <param name="pAttachmentPath">Sciezka do załącznika</param>
        /// <returns></returns>
        public static bool SendEmail(
            string pGmailEmail,
            string pGmailPassword,
            string pFromEmail,
            string pFromName,
            string pTo,
            string pSubject,
            string pBody,
            string pAttachmentPath,
            bool async,
            string bccMail)
        {
            try
            {
                MailMessage myMail = new MailMessage();
                string from = pGmailEmail;
                string fromName = "Fundacja Netcamp";
                if (pFromEmail != string.Empty)
                {
                    from = pFromEmail;
                }
                if (pFromName != string.Empty)
                {
                    fromName = pFromName;
                }
                myMail.From = new MailAddress(from, fromName);
                if (bccMail != string.Empty)
                {
                    myMail.Bcc.Add(bccMail);
                }
                myMail.To.Add(new MailAddress(pTo));
                myMail.Subject = pSubject;
                myMail.SubjectEncoding = new System.Text.UTF8Encoding();
                myMail.BodyEncoding = new System.Text.UTF8Encoding();
                myMail.Body = pBody;
                if ((pAttachmentPath != string.Empty) && System.IO.File.Exists(pAttachmentPath))
                {
                    Attachment MyAttachment =
                            new Attachment(pAttachmentPath);
                    myMail.Attachments.Add(MyAttachment);
                }
                SmtpClient Smtp = new SmtpClient("smtp.office365.com", 587);
                
                Smtp.UseDefaultCredentials = false;
                Smtp.EnableSsl = true;
                Smtp.Credentials = new System.Net.NetworkCredential(pGmailEmail, pGmailPassword);
                if (async)
                {
                    Smtp.SendAsync(myMail, true);
                }
                else
                {
                    Smtp.Send(myMail);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
